/**
 * Foxit Software Inc.
 * Copyright(C) 2005-2014, all rights reserved.
 * The following code is copyrighted and contains proprietary information
 * and trade secrets of Foxit Software Inc.

 * Description: Brief support tester.
 * Author: shaojie zhuo
 * Update: shaojie zhuo (2014-06-25 17:02)
 */
define(function (require, exports, module) {
    var WebPDF = require('./WebPDF');

    var tests = {},
        prefixes = ' -webkit- -moz- -o- -ms- '.split(' '),
        docElement = document.documentElement,
        mod = 'modernizr',
        modElem = document.createElement(mod),
        mStyle = modElem.style;

    function injectElementWithStyles(rule, callback, nodes, testnames) {
        var style, ret, node, docOverflow="",
            div = document.createElement('div'),
            body = document.body,
            fakeBody = body || document.createElement('body');

        if (parseInt(nodes, 10)) {
            while (nodes--) {
                node = document.createElement('div');
                node.id = testnames ? testnames[nodes] : mod + (nodes + 1);
                div.appendChild(node);
            }
        }

        style = ['&#173;', '<style id="s', mod, '">', rule, '</style>'].join('');
        div.id = mod;
        (body ? div : fakeBody).innerHTML += style;
        fakeBody.appendChild(div);
        if (!body) {
            fakeBody.style.background = '';
            fakeBody.style.overflow = 'hidden';
            docOverflow = docElement.style.overflow;
            docElement.style.overflow = 'hidden';
            docElement.appendChild(fakeBody);
        }

        ret = callback(div, rule);
        if (!body) {
            fakeBody.parentNode.removeChild(fakeBody);
            docElement.style.overflow = docOverflow;
        } else {
            div.parentNode.removeChild(div);
        }

        return !!ret;
    }

    function setCss( str ) {
        mStyle.cssText = str;
    }

    function setCssAll( str1, str2 ) {
        return setCss(prefixes.join(str1 + ';') + ( str2 || '' ));
    }

    function contains( str, substr ) {
        return !!~('' + str).indexOf(substr);
    }

    tests['touch'] = function () {
        var touchSupport = false;
        if (('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch) {
            touchSupport = true;
        } else {
            injectElementWithStyles(['@media (', prefixes.join('touch-enabled),('), mod, ')',
                '{#modernizr{top:9px;position:absolute}}'].join(''), function (node) {
                touchSupport = node.offsetTop === 9;
            });
        }

        return touchSupport;
    };

    tests['canvas'] = function() {
        var elem = document.createElement('canvas');
        return !!(elem.getContext && elem.getContext('2d'));
    };

    tests['rgba'] = function() {
        setCss('background-color:rgba(150,255,150,.5)');
        return contains(mStyle.backgroundColor, 'rgba');
    };


    $.support.canvas = tests['canvas']();
    $.support.rgba = tests['rgba']();
    $.support.touch = tests['touch']();
	/* Set cross domain supported for IE*/
	$.support.cors = true;
});